<template>
  <view class="tag-selector-overlay" v-if="visible" @click.stop="closeOnMaskClick ? close() : null">
    <view class="tag-selector-container" @click.stop>
      <view class="tag-selector-header">
        <view class="title">选择标签</view>
        <view class="close-btn" @click="close">
          <text class="close-icon">×</text>
        </view>
      </view>
      
      <view class="tag-selector-content">
        <view class="tag-group">
          <view 
            class="tag-item" 
            v-for="(tag, index) in tags" 
            :key="index"
            :class="{ active: selectedTags.includes(tag) }"
            @click="toggleTag(tag)"
          >
            {{ tag }}
          </view>
        </view>
      </view>
      
      <view class="tag-selector-footer">
        <button class="submit-btn" @click="confirm">确定提交</button>
      </view>
      
      <view class="bottom-indicator"></view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue';

// 定义组件属性
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  initialTags: {
    type: Array,
    default: () => []
  },
  closeOnMaskClick: {
    type: Boolean,
    default: true
  },
  maxSelect: {
    type: Number,
    default: 3
  }
});

// 定义事件
const emit = defineEmits(['update:visible', 'confirm', 'cancel']);

// 标签数据
const tags = ref([
  '热门',
  '店长推荐',
  '新品促销',
  '限时折扣',
  '爆款商品',
  '精选好物'
]);

// 已选择的标签
const selectedTags = ref([...props.initialTags]);

// 监听初始标签变化
watch(() => props.initialTags, (newTags) => {
  selectedTags.value = [...newTags];
});

// 切换标签选择状态
const toggleTag = (tag) => {
  const index = selectedTags.value.indexOf(tag);
  if (index > -1) {
    // 如果已选中，则取消选中
    selectedTags.value.splice(index, 1);
  } else {
    // 如果未选中且未超过最大选择数量，则选中
    if (selectedTags.value.length < props.maxSelect) {
      selectedTags.value.push(tag);
    } else {
      uni.showToast({
        title: `最多只能选择${props.maxSelect}个标签`,
        icon: 'none'
      });
    }
  }
};

// 确认选择
const confirm = () => {
  emit('confirm', selectedTags.value);
  close();
};

// 关闭选择器
const close = () => {
  emit('update:visible', false);
  emit('cancel');
};

// 暴露方法给父组件
defineExpose({
  close,
  confirm
});
</script>

<style lang="scss">
.tag-selector-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tag-selector-container {
  width: 100%;
  background-color: #fff;
  border-radius: 24rpx 24rpx 0 0;
  position: absolute;
  bottom: 0;
  overflow: hidden;
  animation: slide-up 0.3s ease;
}

@keyframes slide-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.tag-selector-header {
  position: relative;
  padding: 40rpx 30rpx 20rpx;
  text-align: center;
}

.title {
  font-size: 36rpx;
  font-weight: 500;
  color: #333;
}

.close-btn {
  position: absolute;
  top: 30rpx;
  right: 30rpx;
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-icon {
  font-size: 48rpx;
  color: #999;
  font-weight: 300;
}

.tag-selector-content {
  padding: 30rpx;
}

.tag-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.tag-item {
  width: 30%;
  height: 80rpx;
  background-color: #f5f5f5;
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 30rpx;
}

.tag-item.active {
  background-color: #fff0f0;
  color: #ff4d4f;
  border: 1rpx solid #ff4d4f;
}

.tag-selector-footer {
  padding: 20rpx 30rpx 40rpx;
}

.submit-btn {
  width: 100%;
  height: 90rpx;
  background-color: #ff4d4f;
  color: #fff;
  border-radius: 45rpx;
  font-size: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bottom-indicator {
  width: 120rpx;
  height: 8rpx;
  background-color: #ddd;
  border-radius: 4rpx;
  margin: 20rpx auto 30rpx;
}
</style>